/**
 * filedir为图片绝对路径
 */
const $fs = require('fs');

var base64img = (filedir)=>{	//将图片转换为base64格式
	let data = $fs.readFileSync(filedir)
	let ext = $path.extname(filedir) , mineType
	switch (ext) {
		case '.jpg':
			mineType = "image/jpeg"
			break;
		case '.png':
			mineType = "image/png"
			break;
		case '.gif':
			mineType = "image/gif"
			break;
		case '.wbmp':
			mineType = "image/vnd.wap.wbmp"
			break;
	}
	data = new Buffer(data).toString('base64');
	return 'data:' + mineType + ';base64,' + data;
}

//解码
function base64_decode(base64str, file) {
    var bitmap = new Buffer(base64str, 'base64');
    $fs.writeFileSync(file, bitmap);
}